#pragma once

#include <stdint.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <functional>


typedef std::function<void (struct in_addr addr, uint16_t port, void *buff, int len, void *dev)> udpRevFunc_t;

class udpSocket_t {

public:
    udpSocket_t(uint16_t port);
    void sendData(const char *ip, uint16_t port, void *data, uint16_t len);

    void sendDataToLastSocket( void *data, uint16_t len);

    void setDataRevCB( udpRevFunc_t dataCB, void *dev )
    {
        revDataCB = dataCB;
        pDev = dev;
    }


protected:
    udpSocket_t();

    pthread_t updRevTid;
    static void *udpThread(void *p);

    udpRevFunc_t revDataCB = nullptr;
    void *pDev;

    uint16_t buffLen = 65535;
    void *revBuff;

    int fd=0;

    struct sockaddr_in cliAddr;
};
